package cn.saas.test;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.saas.test.greendao.gen.HoleBeanDao;
import cn.saas.test.greendao.gen.TowerBeanDao;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DbManager.getInstance().initGreenDao(this, "bbbb");
        insertMany();
        insertOneToMany();
        queryList();
        TextView mtv = (TextView) findViewById(R.id.tv);
        mtv.setOnClickListener(v -> {
            queryToManyUserToOrder();
        });


    }

    private void queryToManyUserToOrder() {
        List<HoleBean> ordersList;
        TowerBeanDao userDao = DbManager.getInstance().getDaoSession().getTowerBeanDao();
        TowerBean user1 = userDao.queryBuilder().where(TowerBeanDao.Properties.TowerCode.eq("孙悟空")).build().unique();

        //直接通过User对象的getOrders()方法获得此用户的所有订单

        Log.d("aaaa", user1.toString());
    }

    private void queryList() {
        String result = "显示结果为：";
        // 获得表中的所有记录
        TowerBeanDao userDao = DbManager.getInstance().getDaoSession().getTowerBeanDao();
        List<TowerBean> users = userDao.loadAll();
        int i = 0;
        for (TowerBean user : users) {
            i++;
            String res = result + "i=" + i + ",id:" + user.getId() + ",name:11" + ",address:" + user.getTowerCode();
            Log.d("TAG", res);
        }
    }

    private void insertOneToMany() {
        List<HoleBean> orderList = new ArrayList<HoleBean>();
        // 这些数据的来源请参考上一章所讲的内容，因为在上一章中有方法为测试提供数据源
        TowerBeanDao userDao = DbManager.getInstance().getDaoSession().getTowerBeanDao();
        HoleBeanDao ordersDao = DbManager.getInstance().getDaoSession().getHoleBeanDao();
        TowerBean user1 = userDao.queryBuilder().where(TowerBeanDao.Properties.TowerCode.eq("孙悟空")).build().unique();
        TowerBean user2 = userDao.queryBuilder().where(TowerBeanDao.Properties.TowerCode.eq("猪八戒")).build().unique();

        HoleBean order1 = new HoleBean();
        order1.setHoleCode("金箍棒");
        order1.setTowerBean(user1); //设置外键值时，要用setUser()方法，以确保外键值不会出错

        HoleBean order2 = new HoleBean();
        order2.setHoleCode("黄金甲");
        order2.setTowerBean(user1);

        HoleBean order3 = new HoleBean();
        order3.setHoleCode("紫金冠");
        order3.setTowerBean(user1);

        HoleBean order4 = new HoleBean();
        order4.setHoleCode("紫金冠");
        order4.setTowerBean(user2);

        orderList.add(order1);
        orderList.add(order2);
        orderList.add(order3);
        orderList.add(order4);

        ordersDao.insertInTx(orderList);
    }

    private void insertMany() {
        List<TowerBean> users = new ArrayList<>();

        TowerBean user1 = new TowerBean();
        user1.setTowerCode("孙悟空");

        TowerBean user2 = new TowerBean();
        user2.setTowerCode("猪八戒");

        TowerBean user3 = new TowerBean();
        user3.setTowerCode("沙悟净");

        TowerBean user4 = new TowerBean();
        user4.setTowerCode("黑熊怪");

        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);

        TowerBeanDao towerBeanDao = DbManager.getInstance().getDaoSession().getTowerBeanDao();
        towerBeanDao.insertInTx(users);
    }
}